package com.lagou.task14;

import java.util.LinkedList;
import java.util.List;

public class ListMethodTest {
    public static void main(String[] args) {
        List lt1 = new LinkedList();
        System.out.println("lt1 ="  + lt1);

        lt1.add(0,"one");
        System.out.println("lt1 ="  + lt1);
        lt1.add(1,3);
        System.out.println("lt1 ="  + lt1);
        lt1.add(1,"two");
        System.out.println("lt1 ="  + lt1);

        //获取指定位置的元素
        Object pos = lt1.get(0);
        System.out.println("pos = " + pos);
        //强制类型转换一定要谨慎，下面编译能通过，运行报错
//        String i1 =(String) lt1.get(2);//java.lang.ClassCastException

        //查找
        System.out.println(lt1.indexOf("two"));
        System.out.println(lt1.lastIndexOf("one"));

        //set方法
        System.out.println(lt1);
        Object ob1 = lt1.set(2,"three");
        System.out.println("ob1 = " + ob1);
        System.out.println("lt1 = " + lt1);//lt1 = [one, two, three]
//          设置的位置不能超过长度
//        Object ob2 = lt1.set(3,"four");
//        System.out.println("ob2 = " + ob2);
//        System.out.println("lt1 = " + lt1);

        //删除
        lt1.remove(0);
        System.out.println("lt1 = " + lt1);
        lt1.add(1);
        lt1.add(2);
        //截取,子集合和当前集合公用内存空间
        List lt2 = lt1.subList(0,2);
        System.out.println("lt2 = " + lt2);//lt2 = [two, three]
        System.out.println("lt1 = " + lt1);//lt1 = [two, three, 1, 2]
        lt2.remove(0);
        System.out.println("lt2 = " + lt2);//lt2 = [three]
        System.out.println("lt1 = " + lt1);//lt1 = [three, 1, 2]
    }

}
